package com.oa.jixiao.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ImgCodeUtil {
	
	private static final int SIZE = 4;
	  private static final int LINES = 5;
	  private static final int WIDTH = 90;
	  private static final int HEIGHT = 43;
	  private static final int FONT_SIZE = 40;

	  public static Object[] createImage()
	  {
	    String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", 
	      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
	    StringBuffer imgCode = new StringBuffer();

	    BufferedImage image = new BufferedImage(90, 43, 1);

	    Graphics graphic = image.getGraphics();

	    graphic.setColor(Color.WHITE);

	    graphic.fillRect(0, 0, 90, 43);

	    Random ran = new Random();
	    for (int i = 0; i < 4; i++)
	    {
	      int n = ran.nextInt(chars.length);

	      graphic.setColor(getRandomColor());

	      graphic.setFont(new Font(null, 4, 40));

	      graphic.drawString(chars[n], i * 85 / 4, 37);

	      
	      
	      imgCode.append(chars[n]);
	    }

	    for (int i = 0; i < 5; i++)
	    {
	      graphic.setColor(getRandomColor());

	      graphic.drawLine(ran.nextInt(90), ran.nextInt(43), ran.nextInt(90), ran.nextInt(43));
	    }

	    return new Object[] { imgCode.toString(), image };
	  }

	  public static Object[] createImage(int calculate)
	    throws ScriptException
	  {
	    String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

	    int SIZE = 3;
	    StringBuffer imgCode = new StringBuffer();

	    BufferedImage image = new BufferedImage(90, 43, 1);

	    Graphics graphic = image.getGraphics();

	    graphic.setColor(Color.WHITE);

	    graphic.fillRect(0, 0, 90, 43);

	    Random ran = new Random();
	    for (int i = 0; i < SIZE; i++)
	    {
	      int n = ran.nextInt(chars.length);

	      graphic.setColor(getRandomColor());

	      graphic.setFont(new Font(null, 4, 40));
	      if (i % 2 == 0)
	      {
	        graphic.drawString(chars[n], i * 85 / SIZE, 37);

	        imgCode.append(chars[n]);
	      } else {
	        int r = new Random().nextInt(3);
	        if (r == 0) {
	          graphic.drawString("+", i * 85 / SIZE, 37);
	          imgCode.append("+");
	        } else if (r == 1) {
	          graphic.drawString("-", i * 85 / SIZE + 5, 33);
	          imgCode.append("-");
	        } else if (r == 2) {
	          graphic.drawString("×", i * 85 / SIZE, 37);
	          imgCode.append("*");
	        }
	      }
	    }

	    for (int i = 0; i < 5; i++)
	    {
	      graphic.setColor(getRandomColor());

	      graphic.drawLine(ran.nextInt(90), ran.nextInt(43), ran.nextInt(90), ran.nextInt(43));
	    }

	    ScriptEngineManager sem = new ScriptEngineManager();
	    ScriptEngine sEngine = sem.getEngineByName("javascript");
	    String result = (String) sEngine.eval(imgCode.toString());

	    return new Object[] { result, image };
	  }

	  public static Color getRandomColor()
	  {
	    Random ran = new Random();
	    Color color = new Color(ran.nextInt(156), ran.nextInt(156), ran.nextInt(156));
	    return color;
	  }
}
